package com.hotelsystem.reservations.reservations;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import com.google.gson.Gson;
import com.hotelsystem.reservations.reservations.ReservationsTimeEnum;
import com.hotelsystem.reservations.constants.Constants;

import java.util.Date;

import com.hotelsystem.reservations.hotels.Hotel;
import com.hotelsystem.reservations.interfaces.ReservationManagementInterface;
import com.hotelsystem.reservations.persons.Client;
import com.hotelsystem.reservations.rooms.Room;
import com.hotelsystem.reservations.reservations.Reservation;

public class ReservationManagement implements ReservationManagementInterface {

	public boolean createReservation(Date startDate, Date endDate,
			Client client, Hotel hotel) {
		ReservationDetails reservationDetails = client.getReservationDetails();

		Room room = reservationDetails.getRoomByDetails(hotel.getRooms(),
				startDate, endDate, hotel.getReservations());

		if (room == null) {
			return false;
		}

		// creates and confirms reservation
		Reservation reservation = new Reservation(startDate, endDate, client,
				room);

		client.getReservationDetails().confirmReservation(reservation, hotel);

		return true;
	}

	public boolean removeReservation(Reservation reservation, Hotel hotel) {
		return hotel.getReservations().remove(reservation);
	}

	/**
	 * Adds the reservation to a JSon file in Constants.FUTURE_RESERVATIONS_PATH
	 * directory with CurentTimeStamp.json name. Later Pesho must process it to
	 * and move it to the Constants.CURRENT_RESERVATIONS_PATH
	 * 
	 * @param rsrv
	 */
	public void AddReservationToFile(Reservation reservation) {
		File resDir = new File(Constants.FUTURE_RESERVATIONS_PATH);
		if (!resDir.exists())
			resDir.mkdir();
		File newRes = new File(resDir, new Timestamp((new Date()).getTime())
				.toString().replaceAll("\\W", " ") + ".json");
		try (BufferedWriter bw = new BufferedWriter(new FileWriter(newRes))) {
			bw.write(reservation.toJson());
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e); 
		}
	}

	/**
	 * Returns ArrayList with all the reservations in the given path
	 * 
	 * @return
	 */
	public ArrayList<Reservation> GetReservationsFromFile(String path) {
		File resDir = new File(path);
		if (!resDir.exists())
			return null;
		List<File> fls = Arrays.asList(resDir.listFiles());
		ArrayList<Reservation> result = new ArrayList<Reservation>(fls.size());
		for (File fl : fls) {
			try {
				result.add(new Reservation(new String(Files.readAllBytes(Paths
						.get(fl.getPath())))));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public ReservationsTimeEnum checkStatus(Reservation reservation) {
		
		if (new Date().before(reservation.getStartDate())){
			return ReservationsTimeEnum.FUTURE;
		}
		
		else if (reservation.getEndDate().before(new Date())) {
			return ReservationsTimeEnum.PASSED;
		}
		
		else return ReservationsTimeEnum.CURRENT;
	}
	
	public ArrayList<ReservationsTimeEnum> readReservationStatus() {
		
		File resDir = new File(Constants.FUTURE_RESERVATIONS_PATH);
		if (!resDir.exists())
			return null;
		List<File> fls = Arrays.asList(resDir.listFiles());
		ArrayList<Reservation> result = new ArrayList<Reservation>(fls.size());
		for (File fl : fls) {
			try {
				result.add(new Reservation(new String(Files.readAllBytes(Paths
						.get(fl.getPath())))));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		ArrayList<ReservationsTimeEnum> enumList = new ArrayList<ReservationsTimeEnum>();
		
		for (int i = 0; i < result.size(); i++) {
			if (new Date().before((result.get(i).getStartDate())))
					enumList.add(ReservationsTimeEnum.FUTURE);
			else if (result.get(i).getEndDate().before(new Date()))
				enumList.add(ReservationsTimeEnum.PASSED);
			else enumList.add(ReservationsTimeEnum.CURRENT);
		}
		
		return enumList;
	}
	
}
